Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config: Make preset config aware of cpu/memory #3636

Merged
merged 2 commits into from
May 8, 2023

Conversation

praveenkumar
Copy link
Member

Preset property is bound with cpu/memory/bundle and have default
values for it but our set function doesn't have map around those
settings and a user can do following without any error message.

$ crc config set preset microshift
$ crc config set cpus 2
$ crc config set preset openshift

and when user try to perform crc start then error message shown.

With this PR we are making the set function bit self aware of the
preset and it's bound properties (cpu/memory) and make sure if those
property doesn't validated for the preset then unset those so default
values going to take over.

with this PR following now works as expected.

$ curl -X POST -d '{"properties":{"preset": "microshift"}}' --unix-socket ~/.crc/crc-http.sock http:/c/api/config
{"Properties":["preset"]}
$ curl -X POST -d '{"properties":{"cpus": "3"}}' --unix-socket ~/.crc/crc-http.sock http:/c/api/config
{"Properties":["cpus"]}
$ curl -X POST -d '{"properties":{"preset": "openshift"}}' --unix-socket ~/.crc/crc-http.sock http:/c/api/config
{"Properties":["preset"]}
$ curl -X GET --unix-socket ~/.crc/crc-http.sock http:/c/api/config | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   894  100   894    0     0  56924      0 --:--:-- --:--:-- --:--:-- 59600
{
  "Configs": {
    "bundle": "/Users/prkumar/.crc/cache/crc_vfkit_4.12.13_arm64.crcbundle",
    "consent-telemetry": "no",
    "cpus": 4,
    "disable-update-check": true,
    "disk-size": 31,
    "enable-cluster-monitoring": false,
    "enable-experimental-features": false,
    "enable-shared-dirs": true,
    "host-network-access": false,
    "http-proxy": "",
    "https-proxy": "",
    "ingress-http-port": 80,
    "ingress-https-port": 443,
    "kubeadmin-password": "",
    "memory": 9216,

[...]

if _, err := c.Unset(key); err != nil {
return "", err
}
return "", nil
Copy link
Member

@anjannath anjannath May 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we should print a message letting the user know the config is restored to its default, since we won't see the config option in config view ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anjannath Currently also we are not seeing the default configs as part of config view.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, do you prefer to keep it that way, or show a message saying Configuration property '%s' restored to default value '%s' (since you are in the file ;) )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anjannath better to keep it that way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think we need to inform the user that we changed their configuration settings. I'll add that.

@@ -121,7 +121,7 @@ func TestViperConfigLoadDefaultValue(t *testing.T) {

bin, err := os.ReadFile(configFile)
assert.NoError(t, err)
assert.JSONEq(t, `{"cpus":4}`, string(bin))
assert.JSONEq(t, `{}`, string(bin))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe also add a comment here, // default values are not written to config file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@gbraad
Copy link
Contributor

gbraad commented May 8, 2023

Started with:

{
  "consent-telemetry": "yes",
  "cpus": 2,
  "preset": "openshift"
}
PS> crc config set cpus 4

cleared the default:

{
  "consent-telemetry": "yes",
  "preset": "openshift"
}

Note: the default preset remains (?)

PS> crc config set preset microshift

changed the preset:

{
  "consent-telemetry": "yes",
  "preset": "microshift"
}
PS> crc config set cpus 1
Value '1' for configuration property 'cpus' is invalid, reason: requires CPUs >= 2
PS> crc config set cpus 2

does not add the value (as matches default)

PS> crc config set cpus 2
{
  "consent-telemetry": "yes",
  "cpus": 3,
  "preset": "microshift"
}

Now changing to OpenShift again:

PS> crc config set preset openshift
{
  "consent-telemetry": "yes"
}

SO this looks good... though here the default preset was cleared. Earlier it didn't.
Feedback this happened would be appreciated, not high priority... we have #3636.

}
}
cpu := c.Get(CPUs)
if err := validation.ValidateCPUs(cpu.AsInt(), preset); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we also use disk-size? This is now identical, but would suggest to create an issue to follow-up.

Copy link
Contributor

@gbraad gbraad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree to add the 'default values aren't stored' in the test

This PR make sure that if user try to set a default value for a
config property then it just unset that property and default value
is used. It will also allow not to have default value in the viper
config file. Also add `UpdateDefaults` to set and get config handler
so API can also make use of it.

Without this Patch
```
$ crc config set preset microshift
$ crc config set cpus 2
$ crc config set preset openshift
$ cat ~/.crc/crc.json
{
  "cpus": 2,
  "preset": "openshift"
}
```

After this patch `~/.crc/crc.json` going to be empty and openshift
preset is going to use default `cpus` as `4`.
Preset property is bound with cpu/memory/bundle and have default
values for it but our `set` function doesn't have map around those
settings and a user can do following without any error message.
```
$ crc config set preset microshift
$ crc config set cpus 2
$ crc config set preset openshift
```
and when user try to perform `crc start` then error message shown.

With this PR we are making the set function bit self aware of the
preset and it's bound properties (cpu/memory) and make sure if those
property doesn't validated for the preset then unset those so default
values going to take over.

with this PR following now works as expected.
```
$ curl -X POST -d '{"properties":{"preset": "microshift"}}' --unix-socket ~/.crc/crc-http.sock http:/c/api/config
{"Properties":["preset"]}
$ curl -X POST -d '{"properties":{"cpus": "3"}}' --unix-socket ~/.crc/crc-http.sock http:/c/api/config
{"Properties":["cpus"]}
$ curl -X POST -d '{"properties":{"preset": "openshift"}}' --unix-socket ~/.crc/crc-http.sock http:/c/api/config
{"Properties":["preset"]}
$ curl -X GET --unix-socket ~/.crc/crc-http.sock http:/c/api/config | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   894  100   894    0     0  56924      0 --:--:-- --:--:-- --:--:-- 59600
{
  "Configs": {
    "bundle": "/Users/prkumar/.crc/cache/crc_vfkit_4.12.13_arm64.crcbundle",
    "consent-telemetry": "no",
    "cpus": 4,
    "disable-update-check": true,
    "disk-size": 31,
    "enable-cluster-monitoring": false,
    "enable-experimental-features": false,
    "enable-shared-dirs": true,
    "host-network-access": false,
    "http-proxy": "",
    "https-proxy": "",
    "ingress-http-port": 80,
    "ingress-https-port": 443,
    "kubeadmin-password": "",
    "memory": 9216,

[...]
```
@openshift-ci
Copy link

openshift-ci bot commented May 8, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: anjannath, gbraad

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci
Copy link

openshift-ci bot commented May 8, 2023

@praveenkumar: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ci/prow/e2e-crc 824c752 link true /test e2e-crc

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@gbraad
Copy link
Contributor

gbraad commented May 11, 2023

Found this reference to some interesting historical fact:
https://github.com/crc-org/crc/pull/2888/files#r770370496

@cfergeau
Copy link
Contributor

Will need some follow-up work #3652

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants